package zulu.megan;

import java.awt.event.KeyEvent;

import bogus.core.BogusCore;
import bogus.userInput.Input;
import owg.util.Calc;
import owg.util.opengl.ColorF;
import graphics.Shape;
import hubol.Entity;
import hubol.Shake;
import hubol.Sprite;

public class Megan extends Entity{
	private final Look arrow = new Look("arrow"), shrug = new Look("shrug"), read = new Look("read"), crux = new Look("crux"), decomp = new Look("decomp"), normal = new Look("normal"), yay = new Look("yay"), decap = new Look("decap"), ponder = new Look("ponder"), shock = new Look("shock"), car = new Look("car", -15), crash = new Look("crash", -15), wave = new Look("wave"), donate = new Look("donate"), dying = new Look("dying"), dead = new Look("dead"), form = new Look("form");
	private final Sprite wings = new Sprite("angel"), change = new Sprite("change");
	private final Look[] command = new Look[]{normal, yay, decap, ponder, shock, car, crash, donate, dead, dying, wave, crux, decomp, shrug, read, arrow, form};
	
	private final int[] keyboard = new int[]{KeyEvent.VK_Q, KeyEvent.VK_W, KeyEvent.VK_E, KeyEvent.VK_R, KeyEvent.VK_T, KeyEvent.VK_Y, KeyEvent.VK_U, KeyEvent.VK_I, KeyEvent.VK_O, KeyEvent.VK_P};
	
	private double image = 0;
	
	private int selected;
	private boolean angel = false;
	
	private double xsc = 1, ysc = 1;
	private double goalXsc = 1, goalYsc = 1, goalSpd = 1;
	
	private double multiplier;
	
	private boolean cVis = false;
	private double cImg = 0;
	
	private Shake s = new Shake(.1);
	
	private Change myChange = null;
	private int changePhase = 0;
	
	private final double scale = .35;

	public Megan() {
		super(432, 243, 2);
		form();
	}
	
	public void form(){
		selected = command.length - 1;
		multiplier = 0;
		changePhase = 1;
		goal(2, .5, 4);
		begin(new Change(1));
		alarm[0] = 10;
		alarm[1] = 60;
	}
	
	public void goal(double xsc, double ysc, double spd){
		goalXsc = xsc;
		goalYsc = ysc;
		goalSpd = spd;
	}
	
	public void change(){
		if (myChange != null){
			int s = myChange.id;
			if (s != -1)
				selected = s;
			if (myChange.invertAngel)
				angel = !angel;
		}
		myChange = null;
		
		changePhase = 0;
	}
	
	public void begin(Change c){
		myChange = c;
		changePhase = 1;
		goal(2, .5, 4);
		alarm[0] = 20;
	}
	
	public void alarmEvent(int i){
		if (i == 0){
			changePhase = 2;
			goal(1, 1, 2);
		}
		else
			selected = 0;
	}
	
	public int getKey(int i){
		if (i < 10)
			return KeyEvent.VK_0 + i;
		return keyboard[i - 10];
	}
	
	public void step(){
		multiplier = Calc.approach(multiplier, 1, 6);
		
		if (changePhase == 0){
			for (int i=0; i<command.length; i++){
				if (changePhase == 0 && Input.checkFrameKey(getKey(i)))
					begin(new Change(i, Input.checkKey(KeyEvent.VK_CONTROL)));
			}
			if (Input.checkFrameKey(KeyEvent.VK_A) && changePhase == 0)
				begin(new Change(true));
			if (Input.checkFrameKey(KeyEvent.VK_M) && changePhase == 0)
				form();
		}
		
		image += .08;
		while (image >= 3)
			image -= 3;
		
		xsc = Calc.approach(xsc, goalXsc, goalSpd);
		ysc = Calc.approach(ysc, goalYsc, goalSpd);
		
		if (cVis){
			cImg *= 1.25;
			cImg += .2;
			if (cImg >= 2){
				cVis = false;
				cImg = 0;
			}
		}
		
		if (changePhase == 2){
			double dist = Calc.dist(xsc, ysc, goalXsc, goalYsc);
			if (!cVis && dist < .03){
				cVis = true;
				cImg = 0;
			}
			if (dist < .01)
				change();
		}
		
		alarmStep();
	}
	
	public void render(Sprite sprite, double img, double shiftY){
		sprite.render((int)img, x + s.x, y + s.y + (shiftY * yscale), xscale, yscale, 0, ColorF.WHITE);
	}
	
	public void render(Sprite sprite, double img){
		render(sprite, img, 0);
	}
	
	public void render(Sprite sprite){
		render(sprite, image);
	}
	
	public void render(Look look){
		render(look.sprite);
	}
	
	public void render(){
		BogusCore.egl.setColor(ColorF.LTGRAY);
		Shape.drawRectangle(0, 0, 1000, 1000);
		
		Look look = command[selected];
		xscale = (float)(xsc * scale * multiplier);
		yscale = (float)(ysc * scale * multiplier);
		if (angel)
			render(wings, image, look.shiftY);
		render(look);
		if (cVis)
			render(change, cImg);
	}
	
	private class Change{
		public boolean invertAngel = false;
		public int id = -1;
		
		public Change(int id){
			this(id, false);
		}
		
		public Change(boolean angel){
			this(-1, angel);
		}
		
		public Change(int id, boolean angel){
			this.id = id;
			this.invertAngel = angel;
		}
	}
	
	private class Look{
		public Sprite sprite;
		public double shiftY;
		
		public Look(String sprite, double shiftY){
			this.sprite = new Sprite(sprite);
			this.shiftY = shiftY;
		}
		
		public Look(String sprite){
			this(sprite, 0);
		}
	}

}
